package com.xyht.medicine_management_system.modules.medicine.controller;


import com.xyht.medicine_management_system.auth.annotation.MedicineAnnotation;
import com.xyht.medicine_management_system.auth.util.JwtTokenUtil;
import com.xyht.medicine_management_system.common.annotation.RequestPass;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.medicine_management_system.modules.medicine.entity.req.MmsMedicineAddReq;
import com.xyht.medicine_management_system.modules.medicine.entity.req.MmsMedicineDeletedReq;
import com.xyht.medicine_management_system.modules.medicine.entity.req.MmsMedicineExportReq;
import com.xyht.medicine_management_system.modules.medicine.entity.req.MmsMedicineUpdateReq;
import com.xyht.medicine_management_system.modules.medicine.service.MmsMedicineService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 * 药品 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-11-29
 */
@RestController
@RequestMapping("/medicine")
@SecretBody
public class MmsMedicineController {

    @Resource
    private MmsMedicineService medicineService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty("添加商品信息")
    @PostMapping("/add")
    public ResponseResult addMedicine(@RequestBody MmsMedicineAddReq addReq, HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return medicineService.addMedicine(userId,addReq);
    }

    @ApiModelProperty("修改商品信息")
    @PostMapping("/update")
    public ResponseResult updateMedicine(@RequestBody MmsMedicineUpdateReq updateReq){
        return medicineService.updateMedicine(updateReq);
    }

    @ApiModelProperty("删除商品信息")
    @PostMapping("/deleted")
    public ResponseResult deletedMedicine(@RequestBody MmsMedicineDeletedReq deletedReq){
        return medicineService.deletedMedicine(deletedReq);
    }

    @ApiModelProperty("查看商品详情")
    @GetMapping("/id")
    @RequestPass
    public ResponseResult getMedicineDetail(@RequestParam("id") String id,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdOrNullByRequest(request);
        return medicineService.getMedicineDetail(id, userId);
    }

    @ApiModelProperty("查看商品列表")
    @GetMapping("/list")
    @RequestPass
    public ResponseResult getMedicineList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "name",required = false) String name,
                                          @RequestParam(value = "type",required = false) String type,
                                          @RequestParam(value = "isOn",required = false) Integer isOn,
                                          HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdOrNullByRequest(request);
        return medicineService.getMedicineList(name,type,isOn,pageSize,pageNum,userId);
    }

    @ApiModelProperty(value = "药品批量导入")
    @PostMapping("/batch/add")
    public ResponseResult batchAddMedicine(@RequestBody Map<String,Object> map, HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try{
            Future<ResponseResult> future = medicineService.batchAddMedicine(map,user_id);
            while (true){
                if (future.isDone()){
                    commonCodeEnum = future.get();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @ApiOperation("批量导出")
    @PostMapping("/batch/export")
    public ResponseResult exportMedicine(@RequestBody MmsMedicineExportReq medicineExportReq,
                                          HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = medicineService.exportMedicine(medicineExportReq, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiOperation("检查商品是否存在")
    @GetMapping("/checkIsMedicine")
    public ResponseResult checkIsMedicine(@RequestParam String id){
        return medicineService.checkIsMedicine(id);
    }


}

